package com.luyuan.crm.sale.web.converter;

import javax.annotation.Resource;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.springframework.stereotype.Component;

import com.luyuan.crm.promotion.Promotion;
import com.luyuan.crm.promotion.PromotionService;
import com.ruijie.util.StringUtils;

@Component("promotionConverter")
public class PromotionConverter implements Converter {
    @Resource(name = "promotionService")
    private PromotionService promotionService;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (StringUtils.isNullOrEmpty(value)) {
            return null;
        }

        Promotion promotion = promotionService.getPromotionById(value);
        return promotion;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Promotion promotion = (Promotion) value;
        if (promotion == null) {
            return null;
        }

        return promotion.getId();
    }
}
